package leetcode.problems;

import java.util.concurrent.atomic.AtomicInteger;

/**
 * Add Two Numbers
 * 查找字符串中的所有字母组合
 * Created by gmwang on 2018/3/23
 */
public class _0326AddTwoNumbers {
    /**
     * You are given two non-empty linked lists representing two non-negative integers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a linked list.
     You may assume the two numbers do not contain any leading zero, except the number 0 itself.
     Example

     Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)
     Output: 7 -> 0 -> 8
     Explanation: 342 + 465 = 807.

     */
    public static ListNode addTwoNumbers(ListNode l1, ListNode l2) {
        int carry = 0;
        ListNode p, dummy = new ListNode(0);
        p = dummy;
        while (l1 != null || l2 != null || carry != 0) {
            if (l1 != null) {
                carry += l1.val;
                l1 = l1.next;
            }
            if (l2 != null) {
                carry += l2.val;
                l2 = l2.next;
            }
            p.next = new ListNode(carry%10);
            carry /= 10;
            p = p.next;
        }
        return dummy.next;
    }
    public static ListNode addTwoNumbersMy(ListNode l1, ListNode l2) {
        int carry = 0;
        ListNode p, dummy = new ListNode(0);
        p = dummy;
        while(l1 != null || l2 != null){
            carry += l1.val + l2.val;
            l1 = l1.next;
            l2 = l2.next;
            p.next = new ListNode(carry%10);
            carry /= 10;
            p = p.next;
        }
        return dummy.next;
    }
    public static void main(String[] args) {
        ListNode listNode2 = new ListNode(2);
        ListNode listNode4 = new ListNode(4);
        listNode2.next = listNode4;
        ListNode listNode3 = new ListNode(3);
        listNode4.next = listNode3;

        ListNode listNode5 = new ListNode(5);
        ListNode listNode6 = new ListNode(6);
        listNode5.next = listNode6;
        ListNode listNode44 = new ListNode(4);
        listNode6.next = listNode44;
        ListNode listNode = addTwoNumbersMy(listNode2, listNode5);
        System.out.println(listNode.val );
        System.out.println(listNode.next.val );
        System.out.println(listNode.next.next.val );
        AtomicInteger atomicInteger = new AtomicInteger();
        atomicInteger.compareAndSet(1,2);
    }
    public static class ListNode {
      int val;
      ListNode next;
      ListNode(int x) { val = x; }
  }
}
